package com.cskaoyan.user.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.result.UserLoginMsg;
import com.cskaoyan.mall.commons.util.GetUserInfoUtils;
import com.cskaoyan.user.config.Base64ToMultipartFile;
import com.cskaoyan.user.service.IFileuploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * Auth: 杨宇健
 * Time: 2023/1/11 11:47
 * Description: 文件上传
 **/

@RestController
public class FileupLoadController {
    @Autowired
    IFileuploadService fileuploadService;

    // base64编码文件上传
    @PostMapping("user/imgaeUpload")
    public ResponseData storageCreate(@RequestBody Map<String, String> map, HttpServletRequest request) throws IOException {

        String base64 = map.get("imgData");

        final String[] base64Array = base64.split(",");
        String dataUir, data;
        if (base64Array.length > 1) {
            dataUir = base64Array[0];
            data = base64Array[1];
        } else {
            //根据你base64代表的具体文件构建
            dataUir = "data:image/jpg;base64";
            data = base64Array[0];
        }
        MultipartFile file = new Base64ToMultipartFile(data, dataUir);


        UserLoginMsg userInfo = GetUserInfoUtils.getUserInfo(request);
        fileuploadService.storageCreate(file, userInfo);
        return new ResponseUtil<>().setData(null);
    }

}
